package com.hengjun.baccarat;

import com.hengjun.baccarat.net.UpdateManager;
import com.hengjun.baccarat.net.UrlConn;
import com.hengjun.baccarat.utils.ThreadManager;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Activity_CheckRegister extends Activity {
	public static TelephonyManager tm;
	private Thread thread;
	private Button recheck;
	private ProgressBar loading;
	private TextView attention;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_checkreigster);
		recheck = (Button) findViewById(R.id.reCheck);
		loading = (ProgressBar) findViewById(R.id.loading);
		recheck.setVisibility(View.INVISIBLE);
		attention = (TextView) findViewById(R.id.attention);
		attention.setVisibility(View.INVISIBLE);

		ThreadManager.handler = new testHander();
		thread = new Thread(runnable2);
		thread.start();
	}

	private Runnable runnable2 = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			tm = (TelephonyManager) Activity_CheckRegister.this
					.getSystemService(TELEPHONY_SERVICE);
			String checnNum;
			try {
				checnNum = UrlConn.CheckMacAdd(tm.getDeviceId());
				if (checnNum.equals("-1")) {
					Intent _intent = new Intent(Activity_CheckRegister.this,
							Register.class);
					Activity_CheckRegister.this.startActivity(_intent);
					Activity_CheckRegister.this.finish();

				} else {
					Intent _intent = new Intent(Activity_CheckRegister.this,
							Login.class);
					Activity_CheckRegister.this.startActivity(_intent);
					Activity_CheckRegister.this.finish();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				handler.obtainMessage(4).sendToTarget();
			}
		}
	};

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (errorDialog != null) {
			errorDialog.dismiss();
		}
	}

	public void rechecklistener(View v) {
		loading.setVisibility(View.VISIBLE);
		recheck.setVisibility(View.INVISIBLE);
		attention.setVisibility(View.INVISIBLE);
		thread = new Thread(runnable2);
		thread.start();
	}

	private Dialog errorDialog = null;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (msg.what == 4) {
				loading.setVisibility(View.INVISIBLE);
				errorDialog = new Dialog(Activity_CheckRegister.this,
						R.style.MyDialogStyle);
				errorDialog.setContentView(R.layout.erro_dialog);
				Button exit = (Button) errorDialog.findViewById(R.id.button1);
				exit.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						errorDialog.dismiss();
					}
				});
				errorDialog.show();

				recheck.setVisibility(View.VISIBLE);
				attention.setVisibility(View.VISIBLE);
			}
		}
	};
	private UpdateManager update = new UpdateManager(
			Activity_CheckRegister.this);

	@SuppressLint("HandlerLeak")
	private class testHander extends Handler {
		@SuppressLint("HandlerLeak")
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			// 更新
			if (msg.what == 1001) {
				update.showNoticeDialog();
			}
		}
	}
}
